package com.hwadee.ems.controller;

import java.time.LocalDate;

import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hwadee.ems.domain.dto.ApprovalDTO;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.po.Approval;
import com.hwadee.ems.service.IApprovalService;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 审批 前端控制器
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Slf4j
@RestController
@RequestMapping("/approval")
public class ApprovalController {

    @Resource
    private IApprovalService approvalService;

    @PutMapping
    public Result<Void> updateById(@RequestBody ApprovalDTO approvalDTO) {
        if (approvalService.lambdaUpdate()
            .eq(Approval::getId, approvalDTO.getId())
            .set(Approval::getApproverId, approvalDTO.getApproverId())
            .set(Approval::getStatus, approvalDTO.getStatus())
            .set(Approval::getOpinion, approvalDTO.getOpinion())
            .set(Approval::getApprovalDate, LocalDate.now())
            .update()) {

            log.info("审批成功，审批ID：{}，审批人ID：{}，审批结果：{}", approvalDTO.getId(), approvalDTO.getApproverId(), approvalDTO.getStatus());

            return Result.ok();
        } else {
            log.error("审批失败，审批ID：{}，审批人ID：{}，审批结果：{}", approvalDTO.getId(), approvalDTO.getApproverId(), approvalDTO.getStatus());
            
            return Result.error();
        }
    }
    
}
